Let's learn about Polymorphic relationships
Summary:
We creating a Laravel Project with the database, 3 models, and 3 migrations.
NOTE: There is no need to add increment ids on pivot tables unless you plan to access them directly. I include them in the video but if you want to omit them in your projects that will fine.
1. Create a new Laravel installation called polymorphic
2 Create a database with the same name polymorphic
3. Create 3 models, Staff, Product, and Photo; here is a reminder of how we do that below
php artisan make:model Staff -m
php artisan make:model Product -m
php artisan make:model Photo -m
4. Edit your migrations. By default we know the migrations will have $table->id(), but in the video, you might see me having the $table->increments('id'), but no worries they are almost the same thing, behind the scenes the $table->id() is $table->bigIncrements('id') behind the scenes .... don't worry about it just do what I tell you in the bottom.
The Migration create_staff_table.php of the Staff model needs to have the below data.
$table->id()
$table->string('name')
$table->timestamps()5. The same thing for the migration create_products_table.php of the Product model.
$table->id()
$table->string('name')
$table->timestamps()6. For the create_photos_table.php migration of the Photo model, we need this
$table->id()
$table->string('path')
$table->integer('imageable_id')
$table->string('imageable_type')
$table->timestamps()7. php artisan migrate
You need this if you following along and need to practice the Polymorphic relationships, otherwise just watch, but I suggest getting used to it. These types of relationships are not very common but its good to know them.